#!/usr/bin/python2.4
#
# Copyright
"""Program to digest articles from a bbs site.
"""
import sys
import telbbs
from google.python import gflags 

flags = gflags
FLAGS= gflags.FLAGS

flags.DEFINE_string('host', 'mitbbs.com', 'BBS site')
flags.DEFINE_string('board', 'SanFrancisco',
   'MITBBS board name. e.g SanFrancisco, Automobile')
flags.DEFINE_integer('first', 1, 'The first article number')
flags.DEFINE_integer('last', 10, 'The last article number')
flags.DEFINE_integer('retry', 10, 'Retry number')
flags.DEFINE_string('prefix', 'majia',
   'The prefix for output file')

def main(argv):
  argv = FLAGS(argv)           # handles help as well
  start_posting = FLAGS.first 
  last_posting = FLAGS.last
  max_posting_num = last_posting - start_posting + 1 
  max_tries = FLAGS.retry 
  print "Starting to download articles from %s at %s" % (
    FLAGS.board, FLAGS.host)
  for i in range(0, max_tries):
    logfile = telbbs.GetLogName(FLAGS.prefix)
    n = telbbs.ReadArticle(FLAGS.host, FLAGS.board, start_posting, 
                    start_posting + max_posting_num, logfile)
    if n < last_posting:
      start_posting = n 
      max_posting_num = last_posting - n
      print "Something wrong. Restart to download @ %d" % n
    else:
      print "done!"
      return
 
if __name__ == '__main__':
  main(sys.argv)
